package com.atstudy.druid;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/1/12 15:16
 * @copyright: 博为峰
 */
public class Test {
    public static void main(String[] args) throws SQLException, IOException {
//        m1();
        InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
        Properties properties = new Properties();
        properties.load(inputStream);

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(properties.getProperty("url"));
        dataSource.setUsername(properties.getProperty("username"));
        dataSource.setPassword(properties.getProperty("password"));
        //初始化连接数
        dataSource.setInitialSize(Integer.parseInt(properties.getProperty("initialSize")));
        //最大连接数
        dataSource.setMaxActive(Integer.parseInt(properties.getProperty("maxActive")));
        DruidPooledConnection connection1 = dataSource.getConnection();
        System.out.println(connection1);
        connection1.close();

    }

    private static void m1() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/29db");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        //初始化连接数
        dataSource.setInitialSize(10);
        //最大连接数
        dataSource.setMaxActive(10);
        DruidPooledConnection connection1 = dataSource.getConnection();
        System.out.println(connection1);
        connection1.close();

        DruidPooledConnection connection2 = dataSource.getConnection();
        System.out.println(connection2);
        connection2.close();

        DruidPooledConnection connection3 = dataSource.getConnection();
        System.out.println(connection3);
        connection3.close();
    }
}
